-- Desc: BO工序resource信息
alter PROCEDURE getBoProcessResource @begingmt_create DATETIME,
                                     @endgmt_create DATETIME,
                                     @PageIndex INT,
                                     @PageSize INT,
                                     @TotalCount INT OUTPUT,
                                     @BoStatus NVARCHAR(255),
                                     @item_code NVARCHAR(255),
                                     @khdm VARCHAR(50),
                                     @team NVARCHAR(255),
    --批量版编号
                                     @bbhList NVARCHAR(2000)
AS
BEGIN
    SET NOCOUNT ON;

    -- 计算分页的起始位置
    DECLARE @Offset INT = (@PageIndex - 1) * @PageSize;
--------------------------------------------------------------------------------------------
    SELECT a.batch_number          AS 工单号,
           mest.name               AS MES订单类型,
           a.item_code             AS Item_Code,
           a.customer_code         AS 客户代码,
           a.customer_name         AS 客户名称,
           a.mes_order_create_time AS 开单日期,
           a.gmt_modified          AS 工单修改时间,
           b.sn                    AS 序号,
           c.resource_name         AS resource_name,
           b.resource_code         AS resource板编号,
           b.resource_remark       AS resource备注,
           b.gmt_create            AS 创建时间,
           b.gmt_modified          AS 修改时间,
           d.process_name          AS 工序名称
    FROM mes_batch_order a
             LEFT JOIN
         mes_bo_process_resource b ON a.id = b.bo_id
             left join sys_dict mest on mest.value = a.mes_order_type and mest.type = 'MES_ORDER_TYPE'
             left join sys_resource c on c.id = b.resource_id
             left join dbo.mes_bo_process d on b.bo_process_id = d.id

------------------------------------------------------------------------------------------------------


    WHERE (@begingmt_create IS NULL OR a.gmt_create >= @begingmt_create)
      AND (@endgmt_create IS NULL OR a.gmt_create <= @endgmt_create)
      AND (@BoStatus IS NULL OR a.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@item_code IS NULL OR a.item_code = @item_code)
      AND (@khdm IS NULL OR a.customer_code = @khdm)
      and (@team IS NULL OR a.team = @team)
      and (@bbhList IS NULL OR  b.resource_code in (SELECT value FROM STRING_SPLIT(REPLACE(@bbhList, CHAR(13), ' '),' ')))


    order by a.gmt_create desc
    OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY;

    SELECT @TotalCount = COUNT(1)

    ---------------------------------------------------------------------
    FROM mes_batch_order a
             LEFT JOIN
         mes_bo_process_resource b ON a.id = b.bo_id
             left join sys_dict mest on mest.value = a.mes_order_type and mest.type = 'MES_ORDER_TYPE'
             left join sys_resource c on c.id = b.resource_id
             left join dbo.mes_bo_process d on b.bo_process_id = d.id

    -------------------------------------------

    WHERE (@begingmt_create IS NULL OR a.gmt_create >= @begingmt_create)
      AND (@endgmt_create IS NULL OR a.gmt_create <= @endgmt_create)
      AND (@BoStatus IS NULL OR a.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@item_code IS NULL OR a.item_code = @item_code)
      AND (@khdm IS NULL OR a.customer_code = @khdm)
      and (@team IS NULL OR a.team = @team)
      and (@bbhList IS NULL OR  b.resource_code in (SELECT value FROM STRING_SPLIT(REPLACE(@bbhList, CHAR(13), ' '),' ')))

END